$(function(){
  	//初始化
	$.post("proCategory.action",function(data){
    	$("#category").append(data);
    	$("#s_category").append(data);
    });
	$.post("product.action",function(data){
    	$("#s_item").append(data);
    });
	$("#s_price,#s_number").each(function(){
		$(this).keyup(function(){
			if($("#s_price").val()!=""&&$("#s_number").val()!="")
				$("#s_cost").html((parseInt($("#s_price").val())/parseInt($("#s_number").val())).toString());
		});
	});
	$("#s_price,#s_number").numeric({allow:"."});
	
	//执行查询操作，并显示结果
    function PerformSearch()
    {
    	var url="searchStock.action?"+$("#form1").serialize();
		$("#content").show();
		$("#content").dataTable({
			"bJQueryUI": true,
			"sPaginationType": "full_numbers",
			"bProcessing":true,
			"sAjaxSource":url,			
			"bDestroy":true,
			"sDom":'CT<"clear">R<"H"lfr>t<"F"ip>',
			"oTableTools":{
				"sSwfPath":"../../JavaScript/DataTables/extras/TableTools/media/swf/copy_cvs_xls_pdf.swf"
				},
			"oColVis": {
						"buttonText": "Change columns"
					}
		});
    }
    
	//查询库存信息
	$("#search").click(function(){
		var boolform1=true;
  		$("#form1 input").each(function(){
  			if($(this).val()!="")
  				boolform1=false;
  		});
		if($("#category").val()==""&&boolform1)
		{
			$( "#dialog-form4" ).dialog( "open" );
  			if ($.browser.msie) {           
  				$("#dialog-form4").css("height", 100);                
			} 
		}
		else
			PerformSearch();
	});
	
	//编辑商品信息
	var $tr;
  	$("#editOne").live("click", function(){
  		$tr=$(this);
  		var ID=$tr.attr("value");
  		$("#s_id").val(ID);
 		$("#s_item").val($(this).parent().parent().find("td").eq(1).text());
 		$("#s_date").val($(this).parent().parent().find("td").eq(3).text());
 		$("#s_place").val($(this).parent().parent().find("td").eq(4).text());
 		$("#s_number").val($(this).parent().parent().find("td").eq(5).text());
 		$("#s_price").val($(this).parent().parent().find("td").eq(6).text());
 		$("#s_cost").html((parseInt($("#s_price").val())/parseInt($("#s_number").val())).toString());
  		$("#dialog-form1").dialog( "open" );
  		if ($.browser.msie) {           
  			$("#dialog-form1").css("height",300);    
  		}
  	});
    
    //删除商品记录
    $("#deleteOne").live("click", function(){
      var ID=$(this).attr("value");
      $("#d_id").val(ID);
      $( "#dialog-form2" ).dialog( "open" );
      if($.browser.msie) {           
    	  $("#dialog-form2").css("height", 100);                
  	  } 
    });
  	
  	//勾选全部
  	$("#selectAll").click(function(){
  		var checkBool=false;
  		if($(this).attr("checked"))
  			checkBool=true;
  		$(".checkOne").each(function(){
  			$(this).attr("checked",checkBool);
  		});
  	});
  	
  	//删除多项
  	var deleteItems;
  	$("#delete").click(function(){
  		deleteItems="";
  		$(".checkOne").each(function(){
  			if($(this).attr("checked"))
  				deleteItems+=$(this).attr("value")+"#";
  		});
  		if(deleteItems=="")
  		{
  			alert("You have not select the items");
  			return;
  		}
  		deleteItems=deleteItems.substring(0,deleteItems.length-1);
  		var temp=deleteItems.split("#");
  		$("#dialog-form3 p").html(temp.length.toString()+" Stock information will be deleted");
 		$( "#dialog-form3" ).dialog( "open" );
 		if ($.browser.msie) {           
  			$("#dialog-form3").css("height", 100);                
		}
  	});
	
	/*-----提示对话框 -----*/
  	//更新对话框	
  	$( "#dialog-form1" ).dialog({
			autoOpen: false,
			height: 450,
			width: 600,
			modal: true,			
			buttons: {
				"Save": function() {
    						$.post("updateStock.action",$("#form3").serialize(),function(data){
							if(data=="true")
							{
								PerformSearch();
								$( "#dialog-form1" ).dialog( "close" );
							}
							else if(data=="false") alert("Update the Stock information failed");
							else alert("Lost connection to the database");					
							});			
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
		
		//删除对话框
		$( "#dialog-form2" ).dialog({
			autoOpen: false,
			height: 250,
			width: 500,
			modal: true,
			buttons: {
				"Continue": function() {
					$.post("delStock.action",{id:$("#d_id").val()},function(data){
  						if(data=="false")
  							alert("Delete Failed");
  						else if(data=="error")
  							alert("Lost connection to the server");
  						PerformSearch();
  						$( "#dialog-form2" ).dialog( "close" );
  					});
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
				close: function() {
					allFields.val( "" ).removeClass( "ui-state-error" );
				}
		});
		
		//删除多项对话框
		$( "#dialog-form3" ).dialog({
			autoOpen: false,
			height: 250,
			width: 500,
			modal: true,
			buttons: {
				"Continue": function() {						
						$.post("delMoreStock.action",{id:deleteItems},function(data){
  						if(data=="false")
  						{
  							alert("Delete Failed");
  							return;
  						}
  						else if(data=="error")
  						{
  							alert("Lost connection to the server");
							return;
						}
						PerformSearch();
  						$( "#dialog-form3" ).dialog( "close" );
						
  					});
  					
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
				close: function() {
					allFields.val( "" ).removeClass( "ui-state-error" );
				}
		});
	
	//无条件查询对话框
	$( "#dialog-form4" ).dialog({
		autoOpen: false,
		height: 250,
		width: 500,
		modal: true,
		buttons: {
			"Continue": function() {
				PerformSearch();
				$(this).dialog("close");
			},
			Cancel: function() {
				$( this ).dialog( "close" );
			}
		},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
	});
 });